<?php

namespace app\adminmall\controller;
use app\web\model\OrderRightsModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 维权订单列表
 */
class OrderRightsController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new OrderRightsModel();
    }
    /**
     * 订单列表
     * @author [longx] 
     */
    public function index( $page = 1){
		$field='m.*,g.goods_name,g.thumb,g.goods_lanmu,a.store_name ,d.nick_name';
		$where = [];
		if(request()->isPost()){
			$param = input('post.');
			//订单号
			if($param['order_number']){
				$where['m.sn'] = ['like', '%'.$param['order_number'].'%'];
			}
			//商家名称
			if($param['store_name']){
				$where['a.store_name'] = ['like', '%'.$param['store_name'].'%'];
			}	
			//订单状态
			if($param['order_state'] != 0){
				$where['m.order_state'] = $param['order_state'];
			}	
			//下单时间
			if($param['start_time']&&$param['end_time']){
				$where['m.order_time'] = ['between time', [$param['start_time'],$param['end_time']]];
			}
			//收货人姓名
			if($param['receiver']){
				$map['m.receiver'] = ['like', '%'.$param['receiver'].'%'];
			}
			//收货人号码
			if($param['receiver_mobile']){
				$map['m.receiver_mobile'] = ['like', '%'.$param['receiver_mobile'].'%'];
			}
		}
	    $list = Db::name('mall_reforder')->alias('m')
	    ->field($field)
	    ->join('__MALL_ORDER_ITEM__ g','g.order_id=m.id','LEFT')
	    ->join('__MALL_AGENT__ a','a.id=m.agent_id','LEFT')
	    ->join('__MEMBER__ d','d.id=m.user_id','LEFT')
		->where($where)
		->order("m.order_time desc")
	    ->paginate(10);
		
		$list1=array();
		if($list){
			foreach($list as $k=>$v){
				$list1[$v['id']]=$v;
			}

			$order_ids = array_keys($list1);
						
			if($order_ids){
				$order_items = db("mall_order_item")->where("order_id","in",$order_ids)->select();
				foreach($order_items as $k1=>$v1){
					 $list1[$v1['order_id']]['items'][]=$v1;
				}
			}
		}
		$express = getListSiteBname("logistics");
		$this->assign("express",$express);
		$this->assign("list1",$list1);
	    return $this->fetch('index', ['list' => $list]);    
    }


    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Order/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Order/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}